package com.atguigu.gmall.list;

import com.atguigu.gmall.common.util.JSONs;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;

@SpringBootTest
public class Estest {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Test
    void get(){
        aaa aaa = elasticsearchRestTemplate.get("1", aaa.class, IndexCoordinates.of("wei"));
        System.out.println("aaa = " + aaa);



    }

    @Test
    void testDelete(){
        String wei = elasticsearchRestTemplate.delete("2", IndexCoordinates.of("wei"));
        System.out.println("wei = " + wei);
    }
    @Test
    void update(){
        aaa aaa = new aaa(1, "张三三");
        Document document = Document.parse(JSONs.toStr(aaa));
        UpdateQuery query = UpdateQuery.builder("1").
                withDocAsUpsert(true).withDocument(document).build();


        UpdateResponse wei = elasticsearchRestTemplate.update(query, IndexCoordinates.of("wei"));
        System.out.println("wei = " + wei);
    }
    @Test
    void testEs(){
        IndexQuery indexQuery = new IndexQuery();
        aaa aaa = new aaa(2, "李四");
        indexQuery.setId(aaa.getId().toString());
        indexQuery.setObject(aaa);
        String wei = elasticsearchRestTemplate.index(indexQuery, IndexCoordinates.of("wei"));
        System.out.println("wei = " + wei);
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class aaa{
    private Integer id;
    private String name;
}
